//
//  StoreDetailViewController.m
//  HeadYang
//
//  Created by sunmingming on 15/7/18.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "StoreDetailViewController.h"
#import "StoreBarberViewController.h"
#import "UIImageView+AFNetworking.h"

@interface StoreDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak) IBOutlet UIImageView *logoImage;
@property(nonatomic,weak) IBOutlet UITableView *listView;
@property(nonatomic,strong) NSDictionary *storeDetailDic;

@property(nonatomic,weak) UIButton *teleButton;
@property(nonatomic,weak) UIButton *phonButton;

@end

@implementation StoreDetailViewController

@synthesize listView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"锦尚国际";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(self.storeDic){
        self.title = [tooles replaceNullFromDic:self.storeDic withKeyArray:@[@"name"]];
        NSDictionary *para =
        @{@"id":[self.storeDic valueForKey:@"id"],
          @"token":APPALL.myUserItem.token};
        [SVProgressHUD showWithStatus:@"loading..."];
        [AVCloud callFunctionInBackground:@"store" withParameters:para block:^(id object, NSError *error) {
            // 执行结果
            if (!error && object) {
                NSLog(@"avget:%@",object);
                NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
                switch ([[aSucData valueForKey:@"success"] intValue]) {
                    case 0:
                    {
                        [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    }
                        break;
                    case 1:
                    {
                        self.storeDetailDic = [aSucData objectForKey:@"store"];
                        [self.logoImage setImageWithURL:[NSURL URLWithString:[[self.storeDetailDic objectForKey:@"pic"] valueForKey:@"url"]] placeholderImage:nil];
                        [listView reloadData];
                    }
                        break;
                    default:
                        break;
                }
                [SVProgressHUD dismiss];
            } else {
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
            }
        }];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!self.storeDetailDic)
        return 0;
    switch (section) {
        case 0:
            return 4;
        case 1:
            return 1;
        default:
            break;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    return 130.0f;
                case 1:
                    return 40.0f;
                case 2:
                    return 50.0f;
                case 3:
                    return 40.0f;
                default:
                    break;
            }
            break;
        case 1:
            return 44.0f;
        default:
            break;
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                    inde = @"StoreDesID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StoreDes" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                    UITextView *tv2 = (UITextView*)[cell viewWithTag:2];
                    lab1.text = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"name"]];
                    tv2.text = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"intro"]];
                }
                    break;
                case 1:{
                    inde = @"StoreGpsID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StoreGps" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                    lab1.text = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"address"]];
                }
                    break;
                case 2:{
                    inde = @"StoreTagID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StoreTags" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    UIImageView *img1 = (UIImageView*)[cell viewWithTag:1];
                    UIImageView *img2 = (UIImageView*)[cell viewWithTag:2];
                    UIImageView *img3 = (UIImageView*)[cell viewWithTag:3];
                    UIImageView *img4 = (UIImageView*)[cell viewWithTag:4];
                    NSString *tagStr = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"serviceTypes"]];
                    img1.hidden = ![tagStr containsString:@"剪发"];
                    img2.hidden = ![tagStr containsString:@"烫发"];
                    img3.hidden = ![tagStr containsString:@"染发"];
                    img4.hidden = ![tagStr containsString:@"护理"];
                }
                    break;
                case 3:{
                    inde = @"StorePhoneID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorePhone" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    if(!self.teleButton){
                        self.teleButton = (UIButton*)[cell viewWithTag:1];
                        [self.teleButton addTarget:self action:@selector(telePressed:) forControlEvents:UIControlEventTouchUpInside];
                    }
                    if(!self.phonButton){
                        self.phonButton = (UIButton*)[cell viewWithTag:2];
                        [self.phonButton addTarget:self action:@selector(phonPressed:) forControlEvents:UIControlEventTouchUpInside];
                    }
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:{
            inde = @"normalmidid";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NormalCellMid" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lb = (UILabel*)[cell viewWithTag:1];
            lb.text = @"本店理发师";
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)telePressed:(id)sender{
    NSString *phoneStr = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"bizPhone"]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneStr]]];
}

-(void)phonPressed:(id)sender{
    NSString *phoneStr = [tooles replaceNullFromDic:self.storeDetailDic withKeyArray:@[@"phone"]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneStr]]];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return;
    StoreBarberViewController *vc = [StoreBarberViewController new];
    vc.storeDic = self.storeDetailDic;
    [self.navigationController pushViewController:vc animated:YES];
}

@end